package cn.iocoder.yudao.module.alipay.service.lengyanwl;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateTime;
import cn.iocoder.yudao.module.alipay.controller.app.lengyanwl.vo.NamePageRespVO;
import cn.iocoder.yudao.module.alipay.util.HttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.util.StringUtils;
import io.swagger.v3.core.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class NameServiceImpl implements NameService {
    @Override
    public Object getName(NamePageRespVO namePageRespVO) {
        String key = "QSAPKxOAMgxitnJgHzkFnipjCa";

        String url = "https://qqlykm.cn/api/Naming/get?key=" + key + "&surname=" + namePageRespVO.getSurname()
                + "&type=" + namePageRespVO.getType()
                + "&birthday=" + namePageRespVO.getBirthday() + "&sex=" + namePageRespVO.getSex();
        List<String> nameList = new ArrayList<>();
        while (nameList.size() < namePageRespVO.getSize()) {
            getNameApi(url, nameList, namePageRespVO.getSurname(), namePageRespVO.getForbid());
        }
        List<String> collect = nameList.stream().limit(namePageRespVO.getSize()).collect(Collectors.toList());
        System.out.println(collect);
        Map<String, Object> result = new HashMap<>();
        result.put("data", collect);
        result.put("total", collect.size());
        return result;
    }

    @Override
    public Object nameTest(NamePageRespVO namePageRespVO) {
        String key = "QSAPKxOAMgxitnJgHzkFnipjCa";
        String url = "https://qqlykm.cn/api/xingmingceshi/get?key=" + key + "&surname=" + namePageRespVO.getSurname()
                + "&name=" + namePageRespVO.getName();
        String get = HttpUtils.createGet(url);
        JSONObject jsonObject = JSONObject.parseObject(get);
        return jsonObject;
    }

    public List<String> getNameApi(String url, List<String> nameList, String surname, String forbid) {
        String get = HttpUtils.createGet(url);
        JSONObject jsonObject = JSONObject.parseObject(get);
        log.error("------------------jsonObject:", jsonObject);
        if(jsonObject==null){
            return null;
        }
        JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("datalist"));
        if(jsonArray==null){
            return null;
        }
        jsonArray.forEach(obj -> {
            JSONObject json = (JSONObject) obj;
            json.getString("name");
            nameList.add(json.getString("name"));
        });
        List<String> collect = new ArrayList<>();
        if (StringUtils.isNullOrEmpty(forbid)) {
            collect = nameList.stream()
                    .distinct()
                    .collect(Collectors.toList());
        } else {
            collect = nameList.stream()
                    .filter(name -> !name.contains(forbid))
                    .distinct()
                    .collect(Collectors.toList());
        }
        return collect;
    }


}
